package com.wang.ioc.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class AnnotationManager {
	/**
	 * 读取ViewComponent注解值。
	 * @param field
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> loadViewComponentAnnotationVlaue(Field field) throws Exception {
		ViewComponent annotation=field.getAnnotation(ViewComponent.class);
        if(annotation==null){
        	return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();  
        map.put("id", annotation.id());
        map.put("defaultKeyboardState", annotation.defaultKeyboardState());
        map.put("clickEventSource", annotation.clickEventSource());
        map.put("backgroundResource", annotation.backgroundResource());
        map.put("defaultTextValueResId", annotation.defaultTextValueResId());
        map.put("isAutowire", annotation.isAutowire());
        map.put("backgroundColorResId", annotation.backgroundColorResId());
        map.put("backgroundColor", annotation.backgroundColor());
        map.put("defaultTextValue", annotation.defaultTextValue());
        map.put("name", annotation.name());
        map.put("imageResource", annotation.imageResource());
        map.put("parentViewId", annotation.parentViewId());
        map.put("defaultVisibility", annotation.defaultVisibility());
        return map;  
    }
	/**
	 * 通用方式加载应用于Field上的注解值。
	 * @param annotationClass
	 * @param field
	 * @return
	 * @throws Exception
	 */
	public static Map<String, Object> loadAnnotationVlaue(Class annotationClass, Field field) throws Exception {
		Annotation annotation=field.getAnnotation(annotationClass);
		if(annotation==null){
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();  
		Method[] methods = annotation.annotationType().getDeclaredMethods();  
		for (Method method : methods) {  
			Object value = method.invoke(annotation);
			map.put(method.getName(), value);  
		}  
		return map;  
	}
	/**
	 * 读取ViewContainer注解值。
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	public static Map<String,Object> loadViewContainerAnnotationValue(Object obj) throws Exception{
		ViewContainer annotation=obj.getClass().getAnnotation(ViewContainer.class);
        if(annotation==null){
        	return null;
        }
        Map<String, Object> map = new HashMap<String, Object>();  
        map.put("useIoc", annotation.useIoc());
        map.put("useDelayModel", annotation.useDelayModel());
        map.put("useInitingDialog", annotation.useInitingDialog());
        map.put("delayTime", annotation.delayTime());
        return map;  
	}
}
